/*
    This file is part of ciberRatoToolsSrc.

    Copyright (C) 2001-2006 Universidade de Aveiro

    ciberRatoToolsSrc is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    ciberRatoToolsSrc is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include<stdio.h>

#include<qvbox.h>
#include<qobject.h>
#include<qlabel.h>
#include<qhbox.h>
#include<qpixmap.h>
#include<qpainter.h>
#include<string.h>
#include<qfont.h>
#include<qlcdnumber.h>
#include"crqdataview.h"
#include"Comm/crreply.h"

#define COLOR 200

CRQDataView::CRQDataView(CRReply *r, CRLab *l, QString skinFName, 
  QWidget * parent, const char *name = 0 )
	: QWidget( parent, name )

	{
	#ifdef DEBUG
		cout << "CRQDataView::CRQDataView\n";
	#endif

	lab = l;
	reply = r;
	status = 1;
	skin(skinFName);
	setBackgroundColor( QColor(COLOR, COLOR, COLOR) );
	//setBackgroundPixmap( QPixmap( backFile ) );
	paint = new QPainter(this);
	finalTime = reply->parameters->simTime;
	curTime = 0; // Current time
	nRobots = 3; // Number of robots by default

	QLabel *timeLabel = new QLabel( this );
	timeLabel->setText("TIME");
	timeLabel->setAlignment( Qt::AlignCenter);
	timeLabel->setFont( QFont( "Courier", 18, QFont::Bold ) );
	timeLabel->setGeometry(85, 55, 100, 20 ); 
	timeLabel->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );

	QPalette pal;
	QColorGroup cg;
	cg.setColor( QColorGroup::Foreground, QColor( 0, 0, 255 ) );
	cg.setColor( QColorGroup::Background, QColor( COLOR, COLOR, COLOR ) );
	pal.setActive( cg );
	pal.setInactive( cg );
	QLCDNumber *time = new QLCDNumber( this, "Score" );
	//time->setBackgroundColor(QColor(COLOR, COLOR, COLOR));
	time->setDecMode();
	time->setGeometry( 80, 75, 100, 70 );
	time->setNumDigits( 4 );
	time->setSegmentStyle( QLCDNumber::Filled );
	time->setPalette( pal );
	time->setFrameStyle(QFrame::NoFrame);
	connect( this, SIGNAL( actualTime(int) ), time, SLOT( display(int) ) );

	QLabel *scoreLabel = new QLabel( this );
	scoreLabel->setText("SCORE");
	scoreLabel->setAlignment( Qt::AlignCenter);
	scoreLabel->setFont( QFont( "Courier", 14, QFont::Bold ) );
	scoreLabel->setGeometry(900, 0, 75, 30 ); 
	scoreLabel->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );

	QLabel *colLabel = new QLabel( this );
	colLabel->setText("COLLISIONS" );
	colLabel->setAlignment( Qt::AlignCenter );
	colLabel->setFont( QFont( "Courier", 14, QFont::Bold ) );
	colLabel->setGeometry(680, 0, 150, 30 ); 
	colLabel->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );

	QLabel *currentTimeLabel = new QLabel( this );
	currentTimeLabel->setText("TIME" );
	currentTimeLabel->setAlignment( Qt::AlignCenter );
	currentTimeLabel->setFont( QFont( "Courier", 14, QFont::Bold ) );
	currentTimeLabel->setGeometry(600, 0, 90, 30 ); 
	currentTimeLabel->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );

	// Robots processing
	if( lab->grid() != NULL )		// if grid exist
		nRobots = lab->grid()->howManyPositions(); 

	usedId = new int[nRobots];

	for ( int n = 0; n < nRobots; n++ )
		usedId[n] = 0;

	robots.resize( nRobots ); // robot is one vector of CRQRobotInfo
	//setGeometry( 0, 1, 995, 180 );
	setGeometry( 8, 30, 995, 180 );
	}

/*==========================================================================*/
CRQDataView::~CRQDataView()
	{
	#ifdef DEBUG
		cout << "CRQDataView::~CRQDataView\n";
	#endif
	
	
	if(paint != NULL)
		delete paint;

	if(usedId != NULL)
		delete usedId;

	for(int i = 0 ; i < 3 ; i++)
		if(robots[i] != NULL)
			delete robots[i];

	robots.clear();
	}

/*==========================================================================*/
void CRQDataView::skin(QString skinFileName)
	{
	FILE *fp = NULL;

	//cout << "CRQDataView::skin skin name = " << skinFileName << endl;

    fp = fopen(skinFileName, "r");
	if(fp == NULL)
		{
		cerr << "Unable to load file " << skinFileName << endl;
		}
	else
		{
		fscanf(fp, "%s", skinName);
		strcpy(backFile, "skins/");
		strcat(backFile, skinName);
		strcat(backFile, "/scorebg.png");

		strcpy(backFile2, "skins/");
		strcat(backFile2, skinName);
		strcat(backFile2, "/scorebg2.png");

		strcpy(clockImage, "skins/");
		strcat(clockImage, skinName);
		strcat(clockImage, "/clock.png");
		}

	fclose(fp);

	} 

/*==========================================================================*/
void CRQDataView::update( CRRobot *rob )
	{
	if ( (rob->id() > 0) && (rob->id() < (nRobots + 1) ) )
		{
		if( usedId[ rob->id() - 1] == 0 )
			{
			CRQRobotInfo *robot = new CRQRobotInfo( rob, skinName, this, 0 );
			robots[rob->id() - 1] = robot; //Add new robot to vector
			usedId[rob->id() - 1] = 1;	   //Update the var used
			robot->setGeometry( 279, (rob->id()-1) * 50 + 28, 715, 100 );
			curTime = rob->currentTime();
			emit actualTime(rob->currentTime() );
			robot->show();
			robots[rob->id() - 1]->update( rob );
			}
		else
			{
			robots[rob->id() - 1]->update( rob );
			curTime = rob->currentTime();
			robStatus = rob->state();
			emit actualTime(rob->currentTime() );
			}

		if(curTime % 5 == 0)
			QWidget::update();
		}
	}

/*==========================================================================*/

void CRQDataView::paintEvent( QPaintEvent *ev)

	{
//	static int toggle = 1;
//
//	if(curTime != 0 || (curTime == 0 && status))
//		{
//		QWidget::paintEvent(ev);
//		QString str;
//		// imagem inicial
//		strcpy(clockImage2, "skins/");
//		strcat(clockImage2, skinName);
//		strcat(clockImage2, "/clockInit.png");
//
//		//QPainter paint(this);		 // para o desenho do relogio
//
//		double quot = (double)curTime / finalTime;
//		str.setNum(curTime);
//					
//		//QBrush br(QColor((int)(255 * quot), (int)(255 - 224 * quot), 0));
//		//paint.setPen(QColor((int)(255 * quot), (int)(255 - 224 * quot), 0));
//
//	#ifdef TOOLS   // desenha o relogio 
//		if(strcmp(skinName, "default") == 0)
//	#else          // desenha a ampulheta
//		if(strcmp(skinName, "default") != 0)
//	#endif
//			{
//			QBrush br;
//			br.setPixmap(QPixmap(clockImage));
//			paint->setPen(QColor(0, 0, 0));
//
//			quot *= 360;
//			//quot *= 335;
//
//			//QBrush br(QColor(242, 220, 145));
//
//			paint->setBrush(br);
//			//paint.setPen(QColor(242, 220, 145));
//			paint->drawPie(23,53,110,110, 90 * 16-(int)(quot *16),
//			  (int)(quot *16));
//			}
//		else
//			{
//			if( curTime == 0 )
//				{
//				setBackgroundPixmap( QPixmap(backFile2) );
//				toggle = 1;
//				status = 0;
//				/*QBrush br;
//				br.setPixmap(QPixmap(clockImage2));
//				paint.setPen(QColor(25, 115, 10));
//				
//				paint.setBrush(br);
//				paint.drawRect(55, 50, 100, 120);
//				*/
//				
//				}
//			else
//				{
//				if(toggle)
//					{
//					setBackgroundPixmap( QPixmap(backFile) );
//					toggle = 0;
//					}
//
//				QBrush br;
//				if(robStatus == CRRobot::STOPPED)	
//					br.setPixmap(QPixmap(clockImage2));
//
//				else
//					br.setPixmap(QPixmap(clockImage));
//
//				paint->setPen(QColor(25, 115, 10));
//				paint->setBrush(br);
//				paint->drawRect(55, 50 + (int)(quot*60), 78,
//				  120 - (int)(quot*120));
//				status = 1;
//				}
//
//			}
//
//
//		// Tempo em formato digital
//		paint->setPen(QColor(255, 255, 255));
//		paint->setFont(QFont("Times", 14, QFont::Bold));
//		paint->drawText(50, 60, 100, 25, Qt::AlignCenter, str);
//		status = 0;
//		//QPainter dblBufferPainter(this);
//		//dblBufferPainter.drawPixmap(0, 0, *dblBuffer);
//		}
//
	}

